//
//  PictureBrowserViewController.swift
//  Whiteboard
//
//  Created by point on 2018/1/10.
//  Copyright © 2018年 point. All rights reserved.
//

import UIKit
import SnapKit
private let kcellID = "kcellID"

class PictureBrowserViewController: UIViewController {
    
    fileprivate var modelArr = [String]()
    fileprivate var indexPath : IndexPath
    
    lazy var collectionView : UICollectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: PictureBrowserCollectionViewLayout())
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        // collectionView.scrollToItem(at: indexPath, at: .left , animated: false)
    }
    
    override func loadView() {
        super.loadView()
        view.frame.size.width += 10
    }
    
    init(indexPath : IndexPath, modelArr : [String]) {
        self.modelArr = modelArr
        self.indexPath = indexPath
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

// MARK:- 交互
extension PictureBrowserViewController {
    @objc fileprivate func saveBtnClick() {
        // 1.获取当前正在显示的image
        let cell = collectionView.visibleCells.first as! PictureBrowserCollectionViewCell
        guard let image = cell.imgView.image else {
            return
        }
        
        // 2.将image对象保存相册
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
        //self.hudOK()
    }
}

// MARK:- 自定义代理
extension PictureBrowserViewController:PictureBrowserCollectionViewCellDelegate {
    func pictureBrowserCollectionViewCellDissMiss() {
        dismiss(animated: true, completion: nil)
    }
}

// MARK:- 原生代理
extension PictureBrowserViewController: UICollectionViewDelegate , UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.modelArr.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kcellID, for: indexPath) as! PictureBrowserCollectionViewCell
        cell.config(model: self.modelArr[indexPath.item])
        
        cell.delegate = self
        return cell
    }
}

// MARK:- 自定义布局
class PictureBrowserCollectionViewLayout : UICollectionViewFlowLayout {
    override func prepare() {
        super.prepare()
        
        // 1.设置itemSize
        itemSize = collectionView!.frame.size
        minimumInteritemSpacing = 0
        minimumLineSpacing = 0
        scrollDirection = .horizontal
        
        // 2.设置collectionView的属性
        collectionView?.isPagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = true
        collectionView?.showsVerticalScrollIndicator = true
    }
}

// MARK:- 设置UI
extension PictureBrowserViewController {
    fileprivate func setupUI(){
        view.addSubview(collectionView)
        collectionView.frame = view.bounds
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.backgroundColor = UIColor.clear
        collectionView.register(PictureBrowserCollectionViewCell.self, forCellWithReuseIdentifier: kcellID)
        collectionView.bounces = false
        
        //        let saveBtn = UIButton(type: .custom)
        //        saveBtn.setTitle("留着撸", for: .normal)
        //        saveBtn.backgroundColor = kColorOrange
        //        saveBtn.alpha = 0.5
        //        saveBtn.layer.cornerRadius = 4;
        //        saveBtn.addTarget(self, action:#selector(saveBtnClick), for: .touchUpInside)
        //        
        //        view.addSubview(saveBtn)
        //        saveBtn.snp.makeConstraints { (make) in
        //            make.bottom.equalTo(0)
        //            make.right.equalTo(0)
        //            make.size.equalTo(CGSize(width: 150, height: 60))
        //        }
    }
}
